`
https://leetcode.cn/problems/maximum-candies-allocated-to-k-children/
`

/**
 * @param {number[]} candies
 * @param {number} k
 * @return {number}
 */
var maximumCandies = function (candies, k) {
  const sum = candies.reduce((acc, cur) => acc + cur)
  if (sum < k) return 0

  // 能不能分出 k 堆 low 个糖果
  const check = (low) => candies.reduce((acc, cur) => acc + Math.floor(cur / low), 0) >= k

  let left = 0, right = Math.min(Math.max(...candies), Math.floor(sum / k)) + 1
  while (left + 1 < right) {
    const mid = left + Math.floor((right - left) / 2)
    if (check(mid)) {
      left = mid
    } else {
      right = mid
    }
  }
  return left
};